﻿using Dtos;
using IBLL;
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class SignlnBll : ISignlnBll
    {
        private ISignlnDal _dal;
        public SignlnBll(ISignlnDal dal)
        {
            _dal = dal;
        }
        public async Task<int> AddSignlnAsync(Guid studentId, int signStatus, int restTime)
        {
            return await _dal.AddAsync(new Model.Signln 
            {
                StudentId = studentId,
                SignStatus = signStatus,
                RestTime = restTime
            });
        }

        public async Task<int> DeleteSignlnAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return data == null ? 0 : await _dal.DeleteAsync(data);
        }

        public async Task<List<SignlnDto>> GetAll()
        {
            var data = await _dal.GetAll();
            return data.Select(s => new SignlnDto 
            {
                Id = s.Id,
                SignStatus = s.SignStatus,
                StudentId = s.StudentId,
                ResTime = s.RestTime
            }).ToList();
        }

        public async Task<SignlnDto> GetSignlnByIdAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return new SignlnDto 
            {
                Id =data.Id,
                StudentId=data.StudentId,
                SignStatus=data.SignStatus,
                UpdateTime = data.UpdateTime,
                CreateTime = data.CreateTime,
            };
        }

        public async Task<List<SignlnDto>> GetSignlnByNameAsync(int restTime)
        {
            var data = await _dal.GetDataByChoose(s => s.SignStatus == restTime);
            if (data != null) 
            {
                return data.Select(s => new SignlnDto
                {
                    Id =s.Id,
                    SignStatus = s.SignStatus,
                    UpdateTime=s.UpdateTime,
                    StudentId = s.StudentId,
                    ResTime=s.RestTime
                }).ToList();
            }
            return null;
        }

        public async Task<int> UpdateSignlnAsync(Guid id, int signStatus, int restTime)
        {
            var data = await _dal.GetById(id);
            if(data == null)
                return 0;
            data.SignStatus = signStatus;
            data.RestTime = restTime;
            data.StudentId = data.StudentId;
            return await _dal.UpdateAsync(data);
        }
    }
}
